﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using DataModel;

namespace FriendsContactWeb
{
    public class DataClass
    {
        static DBTools dbTools = null;
        List<FriendsEvent> dbEvents = null;

        static DataClass()
        {
            dbTools = new DBTools();
        }

        public DataClass(string userName)
        {
            dbEvents = dbTools.GetEventsForUser(userName);
        }

        #region Events

        public List<FriendsEvent> GetAllEvents()
        {
            return dbEvents;
        }

        public List<FriendsEvent> GetEventsForDate(DateTime givenDate)
        {
            if (givenDate.Month < 1 || dbEvents == null)
            {
                return null;
            }
            else
            {
                return dbEvents.Where(f => f.EventDate.Month == givenDate.Month)
                               .Where(f => f.EventDate.Year == givenDate.Year).ToList();
            }
        }

        public List<FriendsEvent> GetEventsForDay(DateTime day, string userName)
        {
            if (dbTools == null)
            {
                return null;
            }
            else
            {
                return dbTools.GetEventsForDay(day, userName);
            }
        }

        public void SaveChangesForEvent(decimal eventId, DateTime eventDate, string author, string title, string note)
        { 
            dbTools.SaveChangesForEvent(eventId, eventDate, author, title, note);
            dbEvents = dbTools.GetAllEvents();
        }

        internal decimal AddNewEvent(DateTime eventDate, string author, string title, string note)
        {
            FriendsEvent newEvent = dbTools.AddNewEvent(eventDate, author, title, note);
            dbEvents.Add(newEvent);
            return newEvent.Id;
        }

        internal void DeleteEvent(decimal eventId)
        {
            dbTools.DeleteEvent(eventId);
            dbEvents = dbEvents.Where(f => f.Id != eventId).ToList();
        }

        internal FriendsEvent GetEventFromId(decimal eventId)
        {
            return dbTools.GetEventFromId(eventId);
        }

        #endregion Events

        #region Users

        internal string GetUserNameFromEvent(FriendsEvent item)
        {
            return dbTools.GetUserNameFromEvent(item);
        }

        #endregion Users

        #region Messages

        internal List<UserMessages> GetMessagesForUser(string userName)
        {
            return dbTools.GetMessagesForUser(userName);
        }

        internal void SendMessageToOtherUsers(decimal eventId, string senderName)
        { 
            dbTools.SendMessageToOtherUsers(eventId, senderName);
        }

        internal void DeleteMessage(decimal msgId)
        {
            dbTools.DeleteMessage(msgId);
        }

        internal void MarkMessageAsRead(int selectedMsgId)
        {
            dbTools.MarkMessageAsRead(selectedMsgId);
        }

        internal void MarkMessageFromFeedback(int selectedMsgId, bool isAccepted)
        {
            dbTools.MarkMessageFromFeedback(selectedMsgId, isAccepted);
        }

        #endregion Messages

        #region Feedback

        internal void SendFeedback(decimal eventId, string userName, bool isAccepted, string note)
        {
            dbTools.SendFeedback(eventId, userName, isAccepted, note);
        }

        internal void AddNewEventFromFeedback(decimal eventId, string userName)
        {
            FriendsEvent newEvent = dbTools.AddNewEventFromParent(eventId, userName);
            dbEvents.Add(newEvent);
        }

        internal List<UserEventsFeedback> GetFeedbackForEvent(decimal eventId)
        {
            return dbTools.GetFeedbackForEvent(eventId);
        }

        #endregion Feedback
    }
}


